package net.ib.mtalk.ui.start;

import java.util.UUID;

import com.google.protobuf.micro.ByteStringMicro;

import android.content.Context;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.util.Log;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.AccessToken;
import net.ib.mntalk.protocol.Common.AccountAuthToken;
import net.ib.mntalk.protocol.Common.AccountId;
import net.ib.mntalk.protocol.Common.AccountInfo;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Common.DeviceInfo;
import net.ib.mntalk.protocol.Common.MnTalkAccountInfo;
import net.ib.mntalk.protocol.Common.Privilege;
import net.ib.mntalk.protocol.Common.RequestToken;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Common.UUIDSlicePredicate;
import net.ib.mntalk.protocol.Common.UUIDSliceRange;
import net.ib.mntalk.protocol.Common.UserToken;
import net.ib.mntalk.protocol.Session.AcquireAccessTokenReq;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenReq;
import net.ib.mntalk.protocol.Session.AcquireAccountTokenReq;
import net.ib.mntalk.protocol.Session.AcquireRequestTokenReq;
import net.ib.mntalk.protocol.Session.AuthenticateReq;
import net.ib.mntalk.protocol.Session.GetViewsReq;
import net.ib.mntalk.protocol.Session.RegisterUserReq;
import net.ib.mntalk.protocol.Session.SetSvcAgreementReq;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.NetFlowControl;
import net.ib.mtalk.network.task.AcquireAccessTask;
import net.ib.mtalk.network.task.AcquireAccountAuthTask;
import net.ib.mtalk.network.task.AcquireAccountTask;
import net.ib.mtalk.network.task.AcquireRequestTask;
import net.ib.mtalk.network.task.AllAddContactsTask;
import net.ib.mtalk.network.task.AuthenticateTask;
import net.ib.mtalk.network.task.GetViewsTask;
import net.ib.mtalk.network.task.RegisterUserTask;
import net.ib.mtalk.network.task.SetSvcAgreementTask;
import net.ib.mtalk.ui.UIController;
import net.ib.mtalk.ui.inbox.InboxListActivity;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;

public class AccountIdController extends UIController{
	public AccountIdController(Context context, Handler handler) {
		super(context, handler);
	}
	
	public void acquireAccountAuthToServer(String id, String pw) {
		AcquireAccountAuthTokenReq acquireAccountAuthTokenReq = new AcquireAccountAuthTokenReq();
		
		UUIDMsg appUuId = GpbSupport.makeUuidMsg(UUID.fromString("6fca4926-7558-4b09-ab05-6bb51bfd1eef"));
		
		String appSecret = "abcdefghijklmnopqrstuvwxyz";
		
		AccountInfo info = new AccountInfo();
		
		AccountId accountId = new AccountId();
		accountId.setType(Common.MnTalkID);
		accountId.setIdentifier(id);
		
		info.setId(accountId);
		
		MnTalkAccountInfo mtalkInfo = new MnTalkAccountInfo();
		mtalkInfo.setId(id);
		mtalkInfo.setPasswordEnc(pw);
		mtalkInfo.setPasswordPln(pw);
		
		info.setMntalk(mtalkInfo);
		
		acquireAccountAuthTokenReq.setAppUuid(appUuId);
		acquireAccountAuthTokenReq.setAppSecret(appSecret);
		acquireAccountAuthTokenReq.setAccountInfo(info);
		
		AcquireAccountAuthTask task = new AcquireAccountAuthTask(mContext, mUiHandler, acquireAccountAuthTokenReq);
		task.execute();
	}
	
	public void acquireAccountToServer(AccountAuthToken accountAuthToken) {
		AcquireAccountTokenReq aquireAccountTokenReq = new AcquireAccountTokenReq();
		aquireAccountTokenReq.setAccountAuthToken(accountAuthToken);
		
		AcquireAccountTask task = new AcquireAccountTask(mContext, mUiHandler, aquireAccountTokenReq);
		task.execute();
	}
	
	public void acquireRequestTokenToServer(AccountToken accountToken) {
		UUIDMsg appUuId = GpbSupport.makeUuidMsg(UUID.fromString("6fca4926-7558-4b09-ab05-6bb51bfd1eef"));
		String appSecret = "abcdefghijklmnopqrstuvwxyz";

		AcquireRequestTokenReq req = new AcquireRequestTokenReq();
		req.setAppUuid(appUuId);
		req.setAppSecret(appSecret);
		
		Privilege privilege = new Privilege().setTarget(Common.REGISTRATION).setType(Common.WRITE);
		req.addPrivilege(privilege);
		Privilege privilege2 = new Privilege().setTarget(Common.REGISTRATION).setType(Common.READ);
		req.addPrivilege(privilege2);
		Privilege privilege3 = new Privilege().setTarget(Common.MESSAGE).setType(Common.WRITE);
		req.addPrivilege(privilege3);
		Privilege privilege4 = new Privilege().setTarget(Common.MESSAGE).setType(Common.READ);
		req.addPrivilege(privilege4);
		Privilege privilege5 = new Privilege().setTarget(Common.PROFILE).setType(Common.WRITE);
		req.addPrivilege(privilege5);
		Privilege privilege6 = new Privilege().setTarget(Common.PROFILE).setType(Common.READ);
		req.addPrivilege(privilege6);
		Privilege privilege7 = new Privilege().setTarget(Common.BUDDIES).setType(Common.WRITE);
		req.addPrivilege(privilege7);
		Privilege privilege8 = new Privilege().setTarget(Common.BUDDIES).setType(Common.READ);
		req.addPrivilege(privilege8);
		Privilege privilege9 = new Privilege().setTarget(Common.CONTACT).setType(Common.WRITE);
		req.addPrivilege(privilege9);
		Privilege privilege10 = new Privilege().setTarget(Common.CONTACT).setType(Common.READ);
		req.addPrivilege(privilege10);
		Privilege privilege11 = new Privilege().setTarget(Common.ACCOUNT).setType(Common.WRITE);
		req.addPrivilege(privilege11);
		Privilege privilege12 = new Privilege().setTarget(Common.ACCOUNT).setType(Common.READ);
		req.addPrivilege(privilege12);

		String deviceId = Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
		DeviceInfo deviceInfo = new DeviceInfo();
		deviceInfo.setDeviceId(ByteStringMicro.copyFromUtf8(deviceId));
		deviceInfo.setAppUuid(accountToken.getAppId());
		req.setDeviceInfo(deviceInfo);
		
		AcquireRequestTask task = new AcquireRequestTask(mContext, mUiHandler, req);
		task.execute();
	}
	
	public void acquireAccessTokenToServer(RequestToken requestToken, UserToken userToken) {
		AcquireAccessTokenReq req = new AcquireAccessTokenReq();
		req.setUserToken(userToken);
		req.setRequestToken(requestToken);

		AcquireAccessTask task = new AcquireAccessTask(mContext, mUiHandler, req);
		task.execute();
	}
	
	public void registerUserToServer(AccountToken accountToken) {
		RegisterUserReq req = new RegisterUserReq();
		req.setAccountToken(accountToken);
		//req.setUserName(new Common.Name().setFirstName(displayName));			// setdisplayname 불가 => setFirstName 으로 변경 
		RegisterUserTask task = new RegisterUserTask(mContext, mUiHandler, req);
		task.execute();
	}
	
	public void authenticateToServer(AccessToken accessToken) {
		/*AuthenticateReq req = new AuthenticateReq();
		req.setAccessToken(accessToken);
		req.setApiVersion(MTalkConstant.API_VERSION);
		req.setDeviceType(Common.ANDROID);

		AuthenticateTask task = new AuthenticateTask(mContext, mUiHandler, req);
		task.execute();*/
		NetFlowControl.tryGetAuth(mContext, mUiHandler);
	}
	
	/**
	 * Add all Contacts at Server
	 */
	public void addAllContacts(){
		AllAddContactsTask task = new AllAddContactsTask(mContext, mUiHandler);
		task.execute();
	}
	
	public void getAllView(){
		UUIDSliceRange slice_range = new UUIDSliceRange();
		slice_range.setAscending(true);
		slice_range.setMaxCount(InboxListActivity.GET_VIEWS_COUNT);
		UUIDSlicePredicate predicate = new UUIDSlicePredicate();
		predicate.setSliceRange(slice_range);
		GetViewsReq getViewsReq = new GetViewsReq();
		getViewsReq.setPredicate(predicate);
		try{
			GetViewsTask task = new GetViewsTask(mContext, mUiHandler, getViewsReq);
			task.execute();
		}catch(Exception e){
			MTalkLogWriter.e(e);
		}
	}
	
	public void setSvcAgreement()
	{
		SetSvcAgreementReq setSvcAgreementReq = new SetSvcAgreementReq();
		setSvcAgreementReq.setTime(System.currentTimeMillis());
		setSvcAgreementReq.setVersion(GlobalPreferences.getSvcVersion());
		
		SetSvcAgreementTask task = new SetSvcAgreementTask(mContext, mUiHandler, setSvcAgreementReq);
		task.execute();
	}
}
